/**
 * 
 * @param {String} url 
 * @param {Object} params 
 * @param {String} type 
 * @param {Object} headers 
 * 
 * @return {Object} 
 */
function get(url,params,type,headers={}){
    const p = new Promise((resolve,reject)=>{
        let xhr = new XMLHttpRequest()
        xhr.onreadystatechange = ()=>{
            if(xhr.readyState === 4){
                if(xhr.status === 200){
                    let res = xhr.responseText
                    if(type === 'json') return res = JSON.parse(res)
                    resolve(res)
                }else{
                    reject(err)
                }
            }
        }
        let arr = []
        for(let key in params){
            arr.push(`${key}=${params[key]}`)
        }
        xhr.open('get',`${url}?${arr.join('&')}`)
        for(let key in headers){
            xhr.setRequestHeader(key,headers[key])
        }
        xhr.send(null)
    })
    return p
}

/**
 * 
 * @param {String} url 
 * @param {Object} params 
 * @param {String} type 
 * @param {Object} headers 
 * 
 * @return {Object} 
 */
 function post(url,params,type,headers={}){
    const p = new Promise((resolve,reject)=>{
        let xhr = new XMLHttpRequest()
        xhr.onreadystatechange = ()=>{
            if(xhr.readyState === 4){
                if(xhr.status === 200){
                    let res = xhr.responseText
                    if(type === 'json') return res = JSON.parse(res)
                    resolve(res)
                }else{
                    reject(err)
                }
            }
        }
        let arr = []
        for(let key in params){
            arr.push(`${key}=${params[key]}`)
        }
        xhr.open('get',url)
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
        for(let key in headers){
            xhr.setRequestHeader(key,headers[key])
        }
        xhr.send(arr.join('&'))
    })
    return p
}